<?php

namespace app\api\controller;


use app\common\controller\ApiController;
use app\common\Result;
use joncc\attribute\attr\AuthNeedless;
use joncc\attribute\attr\Comment;
use joncc\attribute\attr\PermissionDefine;
use joncc\attribute\attr\PermissionNeedless;
use think\facade\Filesystem;
use think\response\Json;


#[Comment('上传')]
#[AuthNeedless]
#[PermissionNeedless('file')]
#[PermissionDefine('file', '文件上传')]
class Upload extends ApiController
{

    public function file(): Json
    {
        $file = $this->request->file('file');
        if ($file->getSize() > 2 * 1024 * 1024) {
            return Result::fail('文件超过2M');
        }

        $saveName = Filesystem::disk('public')->putFile('file', $file);
        if (false === $saveName) {
            return Result::fail('上传失败');
        }
        return Result::success('/storage/' . $saveName);
    }
}
